﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Joyintech.Common;
using Joyintech.Model.inpiao365;
using Joyintech.BLL.inpiao365;

namespace Joyintech.Web.inpiao365.UserCenter
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// 会员注册事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOk_Click(object sender, ImageClickEventArgs e)
        {
            string strError = string.Empty;

            if (this.txtUserName.Text.Trim() == "" || this.txtUserName.Text.Trim() == "请输入您的邮箱")
            {
                strError += "请输入您的邮箱!";
            }

            if (!PageValidate.IsEmail(this.txtUserName.Text.Trim()))
            {
                strError += "你输入的邮箱格式不正确！";
            }

            if (this.txtPwd.Text.Trim() == "" || this.txtPwd.Text.Trim() == "请输入您的密码")
            {
                strError += "请输入您的密码！";
            }
            else if (this.txtPwd.Text.Trim().Length < 6 || this.txtPwd.Text.Trim().Length > 16)
            {
                strError += "密码长度6-16位！";
            }

            if (this.txtRePwd.Text.Trim() == "" || this.txtRePwd.Text.Trim() == "请重复输入您的密码")
            {
                strError += "请重复输入您的密码！";
            }
            else
            {
                if (this.txtRePwd.Text.Trim().Length < 6 || this.txtRePwd.Text.Trim().Length > 16)
                {
                    strError += "密码长度6-16位！";
                }
                else if (this.txtPwd.Text.Trim() != this.txtRePwd.Text.Trim())
                {
                    strError += "两次密码不一致，请重新输入！";
                }
            }

            if (this.txtCity.Text.Trim() == "" || this.txtCity.Text.Trim() == "请输入您所在的城市")
            {
                strError += "请输入您所在的城市！";
            }

            if (this.txtMobile.Text.Trim() == "" || this.txtMobile.Text.Trim() == "手机号" || this.txtMobile.Text.Trim().Length != 11)
            {
                strError += "请输入您的手机号！";
            }

            if (!strError.Equals(""))
            {
                MessageBox.Show(this, strError);
                return;
            }

            //会员信息提交
            customer model = new customer();

            model.CustomerName = this.txtUserName.Text.Trim();
            model.CustomerPhone = this.txtMobile.Text.Trim();
            model.CustomerPass = Utils.MD5(this.txtPwd.Text.Trim());
            model.CustomerResidence = this.txtCity.Text.Trim();
            model.CustomerEmail = this.txtUserName.Text.Trim();

            int result = customer_BLL.GetInstance().Add(model);

            if (result == 1)
            {
                //产生登录信息，此处同UserLogin.aspx.cs界面的登录事件

                //向Cookies中存储SessionId
                customer_BLL.GetInstance().StoreSessionId(HttpContext.Current.Session.SessionID);

                //向Memcached中存储SessionId对应的当前登录会员实体
                customer_BLL.GetInstance().StoreCurrentCustomer(HttpContext.Current.Session.SessionID, model);

                if (DNTRequest.GetQueryString("RetUrl") != "")
                {
                    Response.Redirect(DNTRequest.GetQueryString("RetUrl"));
                }
                else
                {
                    Response.Redirect("/index.htm");
                }
            }
        }
    }
}